package com.yubest.springboothello.controller;

import com.yubest.springboothello.dto.UserDto;
import com.yubest.springboothello.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: hweiyu
 * @Date: 2023/4/13 9:18
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping(value = "/getById/{id}")
    public UserDto getById(@PathVariable Long id) {
        UserDto dto = userService.getById(id);
        return null == dto ? new UserDto() : dto;
    }

    @PostMapping(value = "/saveOrUpdate")
    public String saveOrUpdate(@RequestBody UserDto dto) {
        if (null == dto.getId()) {
            return "id is empty";
        }
        userService.saveOrUpdate(dto);
        return "ok";
    }

    @DeleteMapping(value = "/deleteById/{id}")
    public String deleteById(@PathVariable Long id) {
        userService.deleteById(id);
        return "ok";
    }
}
